package com.linzm.leetcode.primary.字符串;

import org.junit.Test;

/**
 * @Author zimingl
 * @Date 2022/10/7 1:00
 * @Description: TODO
 */
public class 字符串58II左旋转字符串 {

    public String reverseLeftWords(String s, int n) {
//        if (s == null || s.length() == 0){
//            return null;
//        }
        //选用 StringBuilder 单线程使用，比较快，选不选都行
        StringBuilder sb = new StringBuilder();
        for (int i = n; i < s.length(); i++) {
            sb.append(s.charAt(i));
        }
        for (int i = 0; i < n; i++) {
            sb.append(s.charAt(i));
        }
        return new String(sb);
    }

    @Test
    public void test(){
        String s = "abcdefg";
        int k = 2;
        String s1 = reverseLeftWords(s, k);
        System.out.println(s1);
    }

    public String reverseLeftWords2(String s, int n) {
        int len=s.length();
        StringBuilder sb=new StringBuilder(s);
        reverseString(sb,0,n-1);
        reverseString(sb,n,len-1);
        return sb.reverse().toString();
    }
    public void reverseString(StringBuilder sb, int start, int end) {
        while (start < end) {
            char temp = sb.charAt(start);
            sb.setCharAt(start, sb.charAt(end));
            sb.setCharAt(end, temp);
            start++;
            end--;
        }
    }
}
